package cn.woniu.ecommerceplatform.service.impl;

import cn.woniu.ecommerceplatform.entity.Address;
import cn.woniu.ecommerceplatform.entity.Goods;
import cn.woniu.ecommerceplatform.entity.Orders;
import cn.woniu.ecommerceplatform.mapper.AddressMapper;
import cn.woniu.ecommerceplatform.mapper.GoodsMapper;
import cn.woniu.ecommerceplatform.mapper.OrdersMapper;
import cn.woniu.ecommerceplatform.redis.RedisOrders;
import cn.woniu.ecommerceplatform.service.OrdersService;
import cn.woniu.ecommerceplatform.util.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    @Autowired(required = false)
    private OrdersMapper ordersMapper;

    @Autowired(required = false)
    private AddressMapper addressMapper;

    @Override
    public ResponseResult<?> orderList(String goodName, String orderNum, Integer orderState, String account, Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<Orders> orders = ordersMapper.orderList(goodName, orderNum, orderState, account);
//        PageInfo<Orders> pageInfo = new PageInfo<>(orders);
//        return new ResponseResult<>().ok(pageInfo);
        try {
            List<Orders> orders;
            PageInfo<Orders> ordersPageInfo;
            if(pageNum!=null && pageSize!=null) {
                PageHelper.startPage(pageNum, pageSize);
                orders = ordersMapper.orderList(goodName, orderNum, orderState, account);
                ordersPageInfo = new PageInfo<>(orders);
                if (!(ordersPageInfo.getSize() > 0) && pageNum > 1) {
                    PageHelper.startPage(pageNum - 1, pageSize);
                    orders = ordersMapper.orderList(goodName, orderNum, orderState, account);
                    ordersPageInfo = new PageInfo<>(orders);
                }
            }else {
                orders=ordersMapper.orderList(goodName, orderNum, orderState, account);
                ordersPageInfo=new PageInfo<>(orders);
            }
            if(orders.isEmpty()){
                return new ResponseResult<>().isNull();
            }else {
                return new ResponseResult<>().ok(ordersPageInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult<?> orderAdd(Orders orders) {
        //把数据加入数据库
        int count = ordersMapper.orderAdd(orders);
        if(count > 0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    @Transactional
    public ResponseResult<?> orderByGoodAdd(Integer id,Integer num,String account) {
        Orders orders = new Orders();
        Goods goods = goodsMapper.queryGoodById(id);
        Double goodPrice =goods.getGoodPrice().doubleValue();
        Double discount = goods.getDiscount().doubleValue();
        orders.setGoodId(id);
        orders.setBuyNum(num);
        orders.setOrderNum(UUID.randomUUID().toString().replace("-","").substring(0,7));
        orders.setAccount(account);
        orders.setOrderState(2);
        orders.setComments(0);
        orders.setCreateTime(new Date());
        orders.setActualPay((goodPrice-discount)*num);
        orders.setDeductionAmount(goodPrice);
//        address,order_id,user_account,region,phone,receiver
        Integer repertory = goods.getRepertory();
        if (repertory>=num){ //库存数量大于购买数量才能新增订单
            int i = ordersMapper.orderAdd(orders);
            if(i > 0){
                Integer id1 = ordersMapper.queryIdByOrderNum(orders.getOrderNum());
                Address address = new Address();
                address.setAddress("森都1-1-104");
                address.setOrderID(id1);
                address.setAccount(account);
                address.setRegion("祖安");
                address.setPhone("1846871256544");
                address.setReceiver("吵架小能手");
                addressMapper.insertAddressForOrder(address);
                return new ResponseResult<>().ok();
            }else{
                return new ResponseResult<>().notok();
            }
        } else {
            return new ResponseResult<>().notok("库存不足");
        }

    }

    @Override
    public ResponseResult<?> orderDelete(Orders orders) {
        int count = ordersMapper.orderDelete(orders.getId());
        if(count>0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> batchDelete(List<Integer> ids,String account) {
        int count = ordersMapper.batchDelete(ids);
        if(count >0){
        return new ResponseResult<>().ok();
    }else{
        return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> orderUpdate(Orders orders) {
        //时间戳
        int count = ordersMapper.orderUpdate(orders);
        if(count > 0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> orderDetails(Integer orderId) {
        Orders orders = ordersMapper.orderDetails(orderId);
        if(orders != null){
            return new ResponseResult<>().ok(orders);
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> orderDetailsAdd(Orders orders) {
        //时间戳
        orders.setSendTime(new Date());
        int count = ordersMapper.orderDetailsAdd(orders);
        if(count > 0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> orderUpdateOrderState(Orders orders) {
        int count = ordersMapper.orderUpdateOrderState(orders);
        if(count > 0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

    @Override
    public ResponseResult<?> orderUpdateComments(Orders orders) {
        int count = ordersMapper.orderUpdateComments(orders);
        if(count > 0){
            return new ResponseResult<>().ok();
        }else{
            return new ResponseResult<>().notok();
        }
    }

}
